package com.zcmob.web.admanagement.vo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Pattern;

import javax.servlet.ServletException;

import com.zcmob.web.admanagement.parser.GetInfo;
import com.zcmob.web.admanagement.util.PropsUtils;

public class HtmlUtil {

    private static String detailKeywords = "href=\"/apk/download/";

    public static Pattern pattern = Pattern.compile(detailKeywords);

    public static void fetchContentSummaryFromHomepage(String homeURL, FileWriter local) throws Exception {
        HttpURLConnection connection = null;
        try {
            URL url = new URL(homeURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(30000);
            connection.setReadTimeout(40000);
            connection.setRequestProperty("User-Agent", "MSIE 7.0");
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            String line = null;
            
            while ((line = br.readLine()) != null) {

                if (line.contains(detailKeywords) && !line.contains("<span") && !line.contains("安装")) {
                    System.out.println(line);

                    String tmp = line.trim();
                    String download = "http://nduoa.com" + tmp.substring(6, 36);
                    System.out.println(download);

                    URL downloadurl = new URL(download);

                    HttpURLConnection uc = (HttpURLConnection) downloadurl.openConnection();

                    uc.connect();
                    InputStream is = null;
                    is = uc.getInputStream();
                    RandomAccessFile randomAccessFile = new RandomAccessFile("d:\\apks\\" + tmp.substring(20, 26) + ".apk", "rwd");
                    byte[] buffer = new byte[10240];
                    int length = -1;
                    while ((length = is.read(buffer)) != -1) {
                        randomAccessFile.write(buffer, 0, length);

                    }
                    randomAccessFile.close();
                    is.close();
                    
                    AppInfo appinfo = null;;

                    appinfo = GetInfo.getApkInfo("d:\\apks\\" + tmp.substring(20, 26) + ".apk");
                    
                    local.write(appinfo.getPackagename() + "," + appinfo.getVersion() + "\n");
                    local.flush();
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.disconnect();
                }
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) throws Exception {

        // long l = Calendar.getInstance().getTimeInMillis();
        // String context =
        // cleanHtmlTag("http://www.ellechina.com/celeb/interview/20120820-109389-pn-12.shtml");
        // long l1 = Calendar.getInstance().getTimeInMillis();
        // System.err.println("get html: " + (l1 - l));
        //
        // List<String> stems = processStr2Stems(context);
        // long l2 = Calendar.getInstance().getTimeInMillis();
        // System.err.println("分词: " + (l2 - l1));
        //
        // for (String s : stems) {
        // System.out.println(s);
        // }

        // List<Content> urls =
        // fetchContentSummaryFromHomepage("http://app.ellechina.com/?app=rss&controller=index&action=feed&skip=1");
        // // System.out.println(s);
        // for (Content cotent : urls) {
        // System.out.println(cotent.toString());
        // }

        // System.out.println(cleanHtmlTag("http://www.ellechina.com/luxury/celebrity/20121016-pic-116491.shtml").contains("林熙蕾"));

        // System.out.println(fetchContentDetail("http://www.ellechina.com/hair/style/20121129-pic-121605.shtml"));

        try {
            PropsUtils.load2SystemProperties(new File("F:\\syc_space\\zcmobsvn\\src\\new.properties"));
        } catch (IOException e) {
            
            throw new ServletException(e);
        }
        
        FileWriter local = new FileWriter("d:\\package.txt");
        for (int i=2; i < 500; i++) {
            fetchContentSummaryFromHomepage("http://nduoa.com/cat1?type=1&page=" + i, local);
        }
        
    }

}
